package com.max.utils;

import com.max.entity.MettingRoom;
import com.max.exceptions.BusinessException;
import com.max.vo.MettingRoomListVO;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PublicUtils {


    /**
     * 公共的列表转换方法
     * @param sourceList 源列表
     * @param targetClazz 目标类对象
     * @return 目标列表
     * @param <S> 源
     * @param <T> 目标
     */
    public static <S, T> List<T> copyList(List<S> sourceList, Class<T> targetClazz) {
        if (sourceList == null || sourceList.isEmpty()) {
            return Collections.emptyList();
        }
        List<T> resultList = new ArrayList<>(sourceList.size());

        try {
            for (S s : sourceList) {
                // 根据目标类获取其空参构造，然后使用空参构造创建实例
                T item = targetClazz.getConstructor().newInstance();
                BeanUtils.copyProperties(s, item);
                resultList.add(item);
            }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                 NoSuchMethodException e) {
            throw new BusinessException("系统异常");
        }
        return resultList;
    }
}
